import api from "../../../common/api.js";

var settingInfo = wx.getStorageSync("settingInfo") ? wx.getStorageSync("settingInfo") : {},countdown = 60;;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',//标题
    value: '',//input输入值
    verifyCode: '',//验证码
    isShow: true,//获取验证码按钮显示
    lastTime: '',//倒计时
    noTelpnone: true//判断手机号码是否符合标准
  },
  //输入input值
  inputVal(e) {
    var that =this;
    that.setData({
      value: e.detail.value
    })
    var valName = e.detail.value;
    const title = e.currentTarget.dataset.title;
  //  var arrList='1234567890';
    if (title == '电话'){
      
      if (valName.slice(0,1) != '1'){
        that.setData({
          noTelpnone: false
        })
        return;
      }
      // if (arrList.indexOf(valName.slice(1,2)) < 0) {
      //   that.setData({
      //     noTelpnone: false
      //   })
      //   return;
      // }
      if (e.detail.value.length != 11) {
        that.setData({
          noTelpnone: false
        })
        return;
      }
      // console.log((/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value)))
      // if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value))) {
      //   console.log(!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value)))
      //   that.setData({
      //     noTelpnone: false
      //   })
      // }else{
        that.setData({
          noTelpnone: true
        })
      // }
    }
    

  },
  //获取验证码
  getCode() {
    const that = this;
    api.regSendVerifyCode({ mp: that.data.value }, res => {
      that.settime();
    })
  },
  //输入验证码
  getVerifyCodeCode(e) {
    this.setData({
      verifyCode: e.detail.value
    })
  },
  //清空input
  empty() {
    this.setData({
      value: ''
    })
  },
  doClickUp(e){
    console.log(e)
  },
  //保存
  save(e) {
    const title = e.currentTarget.dataset.title, data = new Object();
    if (title == "姓名") {
      settingInfo.name = this.data.value;
      if (this.data.value == ""){
        this.data.value = "   ";
      }
      data.realname = this.data.value;

    } else if (title == "电话") {
      if (this.data.value == "") {
        this.data.value = "   ";
      }
      settingInfo.tel = this.data.value;
      data.mobile = this.data.value;
      // data.mp = this.data.value;
      // data.verifyCode = this.data.verifyCode;
      

    } else if (title == "主营楼盘") {
      if (this.data.value == "") {
        this.data.value = " ";
      }
      settingInfo.floor = this.data.value;
      data.resblock = this.data.value;

    } else if (title == "门店地址") {
      if (this.data.value == "") {
        this.data.value = " ";
      }
      settingInfo.address = this.data.value;
      data.storeAdress = this.data.value;
    }else if(title=="门店品牌"){
      if (this.data.value == "") {
        this.data.value = " ";
      }
      settingInfo.storeName = this.data.value;
      data.storeName = this.data.value;
    }
        
      api.settingEdit(data, function (res) {
   
        if (res.statusCode == 200) {
          app.globalData.indexPage.getAllnumber().then(()=>{
            wx.navigateBack({
              delta: 1
            })
          })
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var title = options.title;

    this.setData({
      title: title
    })

    var obj = wx.getStorageSync("settingInfo");
    if (obj.name && title == "姓名") {
      if (obj.name == " "){
        obj.name = ""
      }
      this.setData({
        value: obj.name
      })
    } else if (obj.tel && title == "电话") {
      if (obj.tel == " ") {
        obj.tel = ""
      }
      this.setData({
        value: obj.tel
      })
    } else if (obj.address && title == "门店地址") {
      if (obj.address == " ") {
        obj.address = ""
      }
      this.setData({
        value: obj.address
      })
    } else if (obj.floor && title == "主营楼盘") {
      if (obj.floor == " ") {
        obj.floor = ""
      }
      this.setData({
        value: obj.floor
      })
    } else if (obj.storeName && title == "门店品牌") {
      if (obj.storeName == " ") {
        obj.storeName = ""
      }
      this.setData({
        value: obj.storeName
      })
    }
    
    if (title == "电话") {
      wx.setNavigationBarTitle({
        title: '我的电话',
      })
      if (this.data.value.length == 11){
        this.setData({
          noTelpnone: true,
        })
      }else{
      this.setData({
        noTelpnone: false,
      })
      }
    } else {
      wx.setNavigationBarTitle({
        title: '设置' + title,
      })
    }

    

  },
  //验证码倒计时
  settime: function () {
    var that = this;
    if (countdown == 0) {
      that.setData({
        isShow: true
      })
      countdown = 60;
      return;
    } else {
      that.setData({
        isShow: false,
        lastTime: countdown
      })
      countdown--;
    }
    setTimeout(function () {
      that.settime();
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
})
